#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using starLiGHT.Physic.TwoD;
using starLiGHT.Camera.TwoD;
using starLiGHT.Physic;
using starLiGHT.Physic.TwoD;

#endregion

namespace starLiGHT.Sandbox.PhysicCollisionCallback
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        World world;
        Camera2D camera;
        DebugRender debugDraw;
        AABB worldAABB;
        CollisionHandler contactListener;

        const float timeStep = 1.0f / 60.0f;
        const int velocityIterations = 10;
        const int positionIterations = 5;
        const float scale = 5.0f;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            world = new World(new Vector2(0, -10), true);
            PhysicSystem2D.Instance.World = world;

            world.SetWarmStarting(true);
            world.SetContinuousPhysics(true);

            contactListener = new CollisionHandler();
            world.SetContactListener(contactListener);
        }

        protected override void Initialize()
        {
            camera = new Camera2D(worldAABB.lowerBound, new Vector2(800, 600));
            camera.Zoom = scale;

            debugDraw = new DebugRender(GraphicsDevice, camera, worldAABB, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, scale);
            debugDraw.Flags = DebugDraw.DrawFlags.Shape;
            world.SetDebugDraw(debugDraw);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //
            // create a ground body
            //
            {
                RigidBody2D ground = PhysicSystem2D.Instance.createEdge(new Vector2(-50.0f, -50.0f), new Vector2(50.0f, -50.0f));
                ground.addEdgeShape(new Vector2(-50.0f, -50.0f), new Vector2(-50.0f, -30.0f));
                ground.addEdgeShape(new Vector2(50.0f, -50.0f), new Vector2(50.0f, -30.0f));
            }

            {
                float[] xs = { 0.0f, -9.0f, 9.0f };

                for (int i = 0; i < 20; ++i)
                {
                    if (i % 2 == 0)
                    {
                        for (int j = 0; j < xs.Length; ++j)
                        {
                            PolygonShape shape = new PolygonShape();
                            shape.SetAsBox(3.5f, 1.0f);

                            FixtureDef fd = new FixtureDef();
                            fd.shape = shape;
                            fd.density = 2.0f;
                            fd.friction = 0.3f;

                            BodyDef bd = new BodyDef();
                            bd.type = BodyType.DYNAMIC;

                            float x = 0.0f;
                            bd.position = new Vector2(xs[j] + x, -48.00f + 2.54f * i);
                            Body body = world.CreateBody(bd);

                            body.CreateFixture(fd);
                        }
                    }
                    else
                    {
                        PolygonShape shape = new PolygonShape();
                        shape.SetAsBox(12.5f, 1.5f);

                        FixtureDef fd = new FixtureDef();
                        fd.shape = shape;
                        fd.density = 0.75f;
                        fd.friction = 0.3f;

                        BodyDef bd = new BodyDef();
                        bd.type = BodyType.DYNAMIC;

                        bd.position = new Vector2(0f, -48.00f + 2.54f * i);
                        Body body = world.CreateBody(bd);

                        body.CreateFixture(fd);
                    }
                }
            }

        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit();

            if (camera != null) debugDraw.updateCamera(camera, true);

            world.Step(timeStep, velocityIterations, positionIterations);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            world.DrawDebugData();

            base.Draw(gameTime);
        }
    }
}
